/**
 *
 */

var root = "/hub/v1";

var wbUtil = {
    st2:null,
    websocketDc:null,
    websocketWrj: null,
    websocketJk: null,
    initSocket: function(wsUrl, callback){
        let websocket = null;
        let that = this;
        if (typeof WebSocket === 'undefined') {
            console.log('您的浏览器不支持WebSocket')
            return
        } else {
            // console.log('websocket:', url)
            // 打开一个websocket
            websocket = new WebSocket(wsUrl)
            // 建立连接
            websocket.onopen = () => {
                that.onOpen(that, websocket, wsUrl);
            }
            // 客户端接收服务端返回的数据
            websocket.onmessage = (evt) => {
                that.onMessage(evt,websocket, callback);
            }
            // 发生错误时
            websocket.onerror = (evt) => {
                that.onErr(evt, websocket, wsUrl, callback);
            }
            // 关闭连接
            websocket.onclose = (evt) => {
                that.onClose(evt, websocket);

            }
        }



    },
    onOpen(that, websocket, wsUrl){
        // 发送数据
        // this.websocket.send('发送数据')
        console.log('websocket:链接成功, url:'+ wsUrl)
        that.sdheartbeat(websocket)
        // that.heartbeat()
    },
    onMessage(evt,websocket,  callback){
        console.log('websocket返回的数据：', evt)
        var data = JSON.parse(evt.data)
        console.log(data)

        if(callback){
            callback(websocket, data);
        }



        if(data.data){//获取data字段
            // console.log(JSON.stringify(data.data))

        }

        console.log("接收数据:" + JSON.stringify(data) );
    },
    onErr(evt,  websocket,  wsUrl, callback){
        let that = this;
        console.log('websocket错误：', evt)
        setTimeout(function (){
            that.initSocket(wsUrl, callback)
        },5000)
    },
    onClose(evt, websocket){
        console.log('websocket关闭：', evt)
        // setTimeout(initSocket, 1000)
        var data = {
            "data":"client断开连接"
        }
        console.log("client断开连接:" + JSON.stringify(data));

    },
    sdheartbeat: function(websocket){
        let that = this;
        var json = {
            "devType": "0",
            "devid": '0',
            "cmd": 31,
            "data": null
        }
        console.log('发送心跳................')
        websocket.send(JSON.stringify(json))
        this.st2 = setTimeout(function(){
            that.sdheartbeat(websocket)
        }, 10*1000)
    },
    sendWsSpec: function(websocket,jingdu, weidu, gaodu, sendKey,devType, devid, cmd){

        // if(jingdu && !isNaN(jingdu)){
        //     this.sendWs(websocket,'longitude',devType, devid, cmd ,  Number(jingdu))
        // }
        // if(weidu && !isNaN(weidu)){
        //     this.sendWs(websocket,'latitude',devType, devid, cmd , Number(weidu))
        // }
        //
        // if(gaodu && !isNaN(gaodu)){
        //     this.sendWs(websocket,'altitude',devType, devid, cmd ,  Number(gaodu))
        // }
        if(jingdu && !isNaN(jingdu) && weidu && !isNaN(weidu) && gaodu && !isNaN(gaodu)){

            if(!websocket){
                console.log('ws对象不存在', websocket)
                return ;
            }
            var json = {
                "devType": devType,
                "devid": devid,
                "cmd": Number(cmd),
                "data": {
                    "dtime"	: this.getCurrentTime()
                }
            }

            json['data']['longitude'] = Number(jingdu);
            json['data']['latitude'] = Number(weidu);
            json['data']['altitude'] = Number(gaodu);
            console.log("发送数据:" + JSON.stringify(json));
            websocket.send(JSON.stringify(json))

        }


    },
    sendWs: function (websocket, sendKey,devType, devid, cmd , sendValue){

        if(!websocket){
            console.log('ws对象不存在', websocket)
            return ;
        }
        var json = {
            "devType": devType,
            "devid": devid,
            "cmd": Number(cmd),
            "data": {
                "dtime"	: this.getCurrentTime()
            }
        }

        json['data'][sendKey] = sendValue;
        console.log("发送数据:" + JSON.stringify(json));
        websocket.send(JSON.stringify(json))
    },
    getCurrentTime: function (){
        const currentTime = new Date();
        const year = currentTime.getFullYear();
        const month = currentTime.getMonth() + 1; // 月份从0开始，因此需要加1
        const day = currentTime.getDate();
        const hours = currentTime.getHours();
        const minutes = currentTime.getMinutes();
        const seconds = currentTime.getSeconds();
        return year + '.' + month + '.' + day + ' ' + (hours<=9 ? ('0'+hours) : hours) + ':'+(minutes<=9 ? ('0'+minutes) : minutes)+':' + (seconds<=9 ? ('0'+seconds) : seconds);
    }
}
